import * as api from "../../api/mine";
var appInst =  getApp();

Page({
    data: {
        onLine:"N",
        headUrl:"",
        nickName:"",
        userId:"",
        userInfo:{},
        daySaleCount: '--',
        increasePerson: '--',
        increaseSaleCount: '--',
        monthSaleCount: '--',
        mobile:"",
        data:{}
    },
    onShow() {
        this.get_user_info();
        api.myDataTotal()
        .then((res)=>{
            this.setData({
                data:res
            })
        })
    },
    copy_user_id(){
        wx.setClipboardData({
            data: this.data.userId
        });
    },
    get_user_info(){
        api.get_user_info()
            .then((res)=>{

                // wx.showToast({
                //     title: res.avatarUrl,
                //     icon: 'none'
                // });

                this.setData({
                    onLine:res.online,
                    userId:res.userNo,
                    nickName:res.nickName,
                    headUrl:res.avatarUrl,
                    mobile:res.mobile,
                    daySaleCount: '0',
                    increasePerson: '0',
                    increaseSaleCount: '0',
                    monthSaleCount: '0'
                });

                // if(res.online == "Y"){
                //     api.mineCountData()
                //         .then((res)=>{
                //             this.setData({
                //                 daySaleCount: res.daySaleCount,
                //                 increasePerson: res.increasePerson,
                //                 increaseSaleCount: res.increaseSaleCount,
                //                 monthSaleCount: res.monthSaleCount
                //             })
                //         })
                // }
            })
    },
    handle_to_login(){
        if( this.data.onLine=="N"){
            wx.navigateTo({
                url: '/pages/login/login',
            });
        }
    },
    handle_jump_page(e){
        let { url } = e.currentTarget.dataset;
        if( this.data.onLine=="N"){
            wx.navigateTo({
                url: '/pages/login/login',
            });
        }
        else{
            wx.navigateTo({
                url: url
            })
        }
    },
    async bind_choose_avatar(e){
        //await utils.check_auth_tag();
        let avatarUrl = e.detail.avatarUrl;

        console.log( avatarUrl )

        let info = await wx.getFileInfo({
            filePath: avatarUrl
        });
        wx.showLoading({
            title: '图片上传中...',
            mask: true
        });

        if( info.errMsg == "getFileInfo:ok" ){
            // let size = Math.floor( info.size / 1024 );
            // if(size > 500){
            //     wx.showToast({
            //         title: '图片最大500kb',
            //         icon: 'none',
            //         image: '',
            //         duration: 1500,
            //         mask: true
            //     });
            //     return;
            // }
            api.uploadUserAvatar({
                avatarUrl:avatarUrl
            })
                .then(()=>{
                    this.setData({
                        headUrl:avatarUrl
                    });
                    wx.hideLoading();
                })
        }
        else{
            wx.showToast({
                title: '图片上传失败',
                icon: 'none',
                image: '',
                duration: 1500,
                mask: true
            });
        }
    }
})
